#ifndef _I2C_CTRL_H_
#define _I2C_CTRL_H_

#include "gd32f3x0.h"

#define I2C_MAX_WAIT_TIMES  400000
#define I2C_CH              I2C0
#define I2C_SPEED           100000
#define I2C_DEF_ADDR        0xA0

#define I2C_WAIT(wait_condition, err_log) \
{ \
    Waitcounts = 0; \
    while(wait_condition) \
    { \
        Waitcounts++; \
        if(Waitcounts > I2C_MAX_WAIT_TIMES) \
        { \
            ERR_PRINT((err_log)); \
            goto __error__; \
        } \
    } \
}

void    I2C_Config(void);
void    I2C_Restart(void);
void    I2C_BusIsIdle(void);
void    I2C_BytesWrite(uint8_t devAddr, uint8_t subAddr, uint8_t* pByte, uint8_t NumByteToWrite);
void    I2C_ByteWrite(uint8_t devAddr, uint8_t subAddr, uint8_t Byte);
void    I2C_BytesRead(uint8_t devAddr, uint8_t subAddr, uint8_t* pBytes, uint16_t NumByteToRead);
uint8_t I2C_ByteRead(uint8_t devAddr, uint8_t subAddr);

#endif

